﻿namespace ufida.u9.ait.openapi
{
	using System;
	using System.Collections.Generic;
	using System.Text;
    using ufida.u9.ait.openapi.utils;
    using UFIDA.U9.Base.SOB;
    using UFIDA.U9.CBO.Pub.Controller;
    using UFIDA.U9.SM.Ship;
    using UFSoft.UBF.AopFrame;
    using UFSoft.UBF.Business;
    using UFSoft.UBF.Util.Context;

	/// <summary>
	/// ShipApproveExSV partial 
	/// </summary>	
	public partial class ShipApproveExSV 
	{	
		internal BaseStrategy Select()
		{
			return new ShipApproveExSVImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class ShipApproveExSVImpementStrategy : BaseStrategy
	{
		public ShipApproveExSVImpementStrategy() { }

		public override object Do(object obj)
		{						
			ShipApproveExSV bpObj = (ShipApproveExSV)obj;

			try
			{
				if (bpObj.Data == null || bpObj.Data.Count == 0)
					throw new Exception("传入参数为空");
				List<CommonArchiveDataDTOData> retlist = new List<CommonArchiveDataDTOData>();

				//List<object> inputdtos = DTOUtils.DTOListPropCopy(bpObj.Data.ConvertAll(item => item as object)
				//	, typeof(CommonArchiveDataDTOData));
				foreach (var dto in bpObj.Data)
				{
					Ship item = Ship.Finder.Find($"DocNo='{dto.DocNo}'");
					if (item == null)
					{
						//新增逻辑
						throw new Exception($"单号:{dto.DocNo}不存在!");
					}
					else
					{
						//修改逻辑
						if (item.Status != ShipStateEnum.Creates && item.Status != ShipStateEnum.Approving)
							throw new Exception($"单号[{item.DocNo}]状态不为开立或审核中,不能审核!");

                        //如果传入日期,需要先保存出货确认日期
                        if (!String.IsNullOrEmpty(dto.DocDate))
                        {
							using (ISession session = Session.Open())
							{
                                DateTime shipDate = DateTime.Parse(dto.DocDate);
								//出货单确认日期
								item.ShipConfirmDate= shipDate;
								//出货单记账期间
								String periodstr = $"{shipDate.ToString("yyyy-MM")}";
								SOBAccountingPeriod sobperiod = EntityUtils.GetEntityByCode<SOBAccountingPeriod>(periodstr, "DisplayName");
								item.KeepAccountsPeriod= sobperiod;

								foreach (var line in item.ShipLines)
                                {
									//出货单行确认日期
									line.ShipConfirmDate = shipDate;
                                }
                                if (item.SOBPeriods == null || item.SOBPeriods.Count == 0)
                                {
									//出货单记账期间子表
									SOBPeriod period = SOBPeriod.Create(item);
									period.SOBPeriods = sobperiod;
									period.SOB = EntityUtils.getSetofBooks();
									period.Org = item.Org;

                                }
                                else
                                {
									foreach (var sobline in item.SOBPeriods)
									{
										//出货单记账期间子表
										sobline.SOBPeriods = sobperiod;
									}
								}
								session.Commit();
							}
						}

						//这里修改为调用产业链服务
						ShipBase.ApproveDoc(item);

						//返回
						CommonArchiveDataDTOData retdto = new CommonArchiveDataDTOData();
						retdto.ID = item.ID;
						retdto.Code = item.DocNo;
						retdto.Name = item.DocType.Name;
						retlist.Add(retdto);
					}
				}
				return ResultUtils.success(bpObj, bpObj.Data, retlist);
			}
			catch (Exception ex)
			{
				return ResultUtils.error(bpObj, bpObj.Data, ex);
			}
		}		
	}

	#endregion
	
	
}